package com.thlws.rabbitmq.worker;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 工人模式(多劳多得)
 * 无需设置Exchange信息 
 * 注意此时与 routingKey,Exchange 无任何关联
 * 
 * 本质是Producer,Consumer 使用相同QueueName进行数据传输,客户端设置每次仅获取一笔数据.
 * 同一笔数据只会发到一个消费端，可以理解实现了负载均衡方式的消费.
 *
 */
public class Producer {
	
	private final static String QUEUE_NAME = "hello";

	public static void main(String[] args) throws Exception {
		
		ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("116.228.212.36");
        factory.setUsername("hanley");
        factory.setPassword("hanley");
        factory.setVirtualHost("/test");
        try (Connection connection = factory.newConnection();
        	Channel channel = connection.createChannel()) {
        	
	    	 channel.queueDeclare(QUEUE_NAME, false, false, false, null);
	         String message = "Hello World(汤翰林测试数据)";
	         channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
	         System.out.println(" [x] Sent '" + message + "'");
        }

	}

}
